package com.controller;

import com.entity.Order;
import com.service.OrderService;
import com.util.GetDate;
import com.util.KeyUtil;
import com.util.StatusCode;
import com.vo.LayuiPageVo;
import com.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  订单控制器
 * </p>
 *
 * @author LMN KCT
 * @since 2021-7-15
 */
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 订单界面：删除订单
     * 前端传入删除操作（orderoperate：删除）,获取session中用户id信息，判断是否登录
     * (1). 删除商品
     * 1.前端传入商品用户id（userid）、订单id（orderid）
     * 2.session中获取订单用户id（userid）
     * 3.进行删除操作
     */
    @ResponseBody
    @PostMapping("/order/operate")
    public ResultVo insertorder(@RequestBody Order order, HttpSession session) {
        String userid = (String) session.getAttribute("userid");
        Integer orderoperate = order.getOrderoperate();
        order.setUserid(userid);
        if (orderoperate == 1) {
            Integer i = orderService.insertOrder(order);
            if(i==1){
                return new ResultVo(true, StatusCode.OK, "订单已生成");
            }
            return new ResultVo(false, StatusCode.ERROR, "订单生成失败");

        }else {
            Integer i = orderService.updateOrder(order);
            if (i == 1) {
                return new ResultVo(true, StatusCode.OK, "删除成功");
            }
            return new ResultVo(false,StatusCode.ERROR,"删除失败");
        }
    }

    /**
     * 订单列表界面删除订单
     * 1.前端传入订单id（orderid）
     * 2.判断是否本人删除订单
     * 3.进行删除订单操作
     */
    @ResponseBody
    @PutMapping("/order/delete/{orderid}")
    public ResultVo deleteorder(@PathVariable("orderid") String orderid, HttpSession session){
        Integer i = orderService.deleteFromOrder(orderid);
        if (i == 1){
            return new ResultVo(true, StatusCode.OK,"删除成功");
        }
        return new ResultVo(false,StatusCode.ERROR,"删除失败");
    }

    /**
     * 订单列表界面删除订单
     * 1.前端传入订单id（orderid）
     */
    @ResponseBody
    @PostMapping("/order/pay-success")
    public ResultVo paysuccess(@RequestBody Order order,HttpSession session){
        String userid = (String) session.getAttribute("userid");
        String orderid = KeyUtil.genUniqueKey();
        order.setUserid(userid).setOrderid(orderid).setFormtime(GetDate.strToDate()).setOrderstatus(1);
        Integer i = orderService.formRealOrder(order);
        if (i == 1){
            return new ResultVo(true, StatusCode.OK,"支付中……", orderid);
        }
        return new ResultVo(false,StatusCode.ERROR,"支付失败");
    }

    /**
     * 分页查看用户所有订单内容
     * 前端传入页码、分页数量
     * 查询分页数据
     */
    @ResponseBody
    @GetMapping("/user/order/queryall")
    public LayuiPageVo userorder(int limit, int page, HttpSession session) {
        String userid = (String) session.getAttribute("userid");
        List<Order> orderList = orderService.queryAllOrder((page - 1) * limit, limit, userid);
        Integer dataNumber = orderService.queryOrderCount(userid);
        return new LayuiPageVo("",0,dataNumber,orderList);
    }

    /**
     * 分页查看订单详情
     * 前端传入页码、分页数量
     * 查询分页数据
     */
    @ResponseBody
    @GetMapping("/orderdata/{orderid}")
    public LayuiPageVo orderdetail(@PathVariable("orderid") String orderid) {
        List<Order> orderList = orderService.queryAllOrderItems(orderid);
        Integer dataNumber = orderService.queryOrderItemsCount(orderid);
        return new LayuiPageVo("",0,dataNumber,orderList);
    }

    /**
     * 订单详情
     * 根据订单id（orderid）查询订单信息
     * */
    @GetMapping("/order-detail/{orderid}")
    public String product_detail(@PathVariable("orderid") String orderid, ModelMap modelMap, HttpSession session){
        String userid = (String) session.getAttribute("userid");
        modelMap.put("mainOrder",orderService.queryMainOrder(orderid));
        return "user/order/order-detail";
    }

    /**
     * 分页查看用户所有订单内容
     * 前端传入页码、分页数量
     * 查询分页数据
     */
    @ResponseBody
    @GetMapping("/user/order/formorder")
    public LayuiPageVo formorder(HttpSession session) {
        String userid = (String) session.getAttribute("userid");
        List<Order> orderList = orderService.formOrder(userid);
        Integer dataNumber = orderService.queryFormOrderCount(userid);
        return new LayuiPageVo("",0,dataNumber,orderList);
    }
}

